﻿using libemscontrol.act;
using System.Collections.Generic;

namespace libcontrol
{
    public delegate void ControlMsgCallback(InterfaceMsg msg,string strText);
    public class LedControl
    {
        private static LedControl _Instance = null;
        public static LedControl GetInstance()
        {
            if (_Instance == null)
            {
                _Instance = new LedControl();
            }
            return _Instance;
        }

        private bool _connected = false;
        public bool Connected
        {
            get { return _connected; }              
        }

        private CEmsAct act = null;

        private LedControl()
        {
            act = CEmsAct.GetInstance();
        }    

        public bool Connect(string portName)
        {
            _connected = act.Init(portName);
            return _connected;
        }

       /// <summary>
       /// 调用控制接口
       /// </summary>
       /// <param name="InterfaceID">接口ID</param>
        public void ControlInterface(int InterfaceID)
        {
            if (!_connected)
            {
                return;
            }
            var ltInterface = GetInterfaces();
            foreach(var item in ltInterface)
            {
                if (InterfaceID == item.ID)
                {
                    Logger.WriteAppCallLog("调用:"+item.Name);
                    SendMemData.GetInstance().SetBit(InterfaceID);
                }
            }           
        }

        public List<InterfaceMsg> GetInterfaces()
        {
            return act.ltInterface;
        }

        public List<InterfaceMsg> GetMsgs()
        {
            return act.ltMsg;
        }

        /// <summary>
        /// 设置消息回调函数
        /// </summary>
        /// <param name="controlMsgCallback"></param>
        /// <returns></returns>
        public bool SetMsgCallBack(ControlMsgCallback controlMsgCallback)
        {
            RecvMemData.GetInstance().m_delControlBoardMsg = controlMsgCallback;
            act.MachineDoAction(ActionCmd.AC_DATA_SYNC);
            return true;
        }

        public void SetLed1(LedCommand command)
        {
 
            switch (command)
            {
                case LedCommand.Off:
                    ControlInterface((int)IC.IF_OFF1);
                    break;
                case LedCommand.On:
                    ControlInterface((int)IC.IF_ON1);
                    break;
                case LedCommand.Flash:
                    ControlInterface((int)IC.IF_FLASH1);
                    break;
                default:
                    break;
            }            
        }

        public void SetLed2(LedCommand command)
        {
            if(!_connected)
            {
                return;
            }
            switch (command)
            {
                case LedCommand.Off:
                    ControlInterface((int)IC.IF_OFF2);
                    break;
                case LedCommand.On:
                    ControlInterface((int)IC.IF_ON2);
                    break;
                case LedCommand.Flash:
                    ControlInterface((int)IC.IF_FLASH2);
                    break;
                default:
                    break;
            }
        }

        public void SetLed3(LedCommand command)
        {
            if (!_connected)
            {
                return;
            }
            switch (command)
            {
                case LedCommand.Off:
                    ControlInterface((int)IC.IF_OFF3);
                    break;
                case LedCommand.On:
                    ControlInterface((int)IC.IF_ON3);
                    break;
                case LedCommand.Flash:
                    ControlInterface((int)IC.IF_FLASH3);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 关闭串口
        /// </summary>
        public void Close()
        {
            if(Connected)
            {
                act.Close();
                _connected = false;
            }            
            Logger.WriteAppCallLog("动态库退出");
        }
    }
}
